package run.newbug.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import run.newbug.jt.mapper.ItemCatMapper;
import run.newbug.jt.pojo.Item;
import run.newbug.jt.pojo.ItemCat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ItemCatServiceImpl implements ItemCatService{
    @Autowired
    private ItemCatMapper itemCatMapper;

    public Map<Integer, List<ItemCat>> itemLM(){
        Map<Integer, List<ItemCat>> itemListMap = new HashMap<>();
        List<ItemCat> itemCatAllList = itemCatMapper.selectList(null);
        for (ItemCat x : itemCatAllList){
            int parentId = x.getParentId();
            if(itemListMap.containsKey(parentId)){
                List<ItemCat> exitList = itemListMap.get(parentId);
                exitList.add(x);
//                System.out.println("Exit__"+itemListMap);
            }else {
                List<ItemCat> firstList = new ArrayList<>();
                firstList.add(x);
                itemListMap.put(parentId, firstList);
//                System.out.println("First__"+itemListMap);
            }
//            itemListMap.put(x.getParentId(), );
        }
//        System.out.println(itemListMap);
        return itemListMap;
    }

    @Override
    public List<ItemCat> getItemCatList(Integer level) {
        long startTime = System.currentTimeMillis();
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0 );
        List<ItemCat> itemCatsOne = itemCatMapper.selectList(queryWrapper);
        if(level == 3){
            for (ItemCat x1 : itemCatsOne){
                queryWrapper.clear();
                queryWrapper.eq("parent_id", x1.getId());
                List<ItemCat> itemCatsTwo = itemCatMapper.selectList(queryWrapper);
                x1.setChildren(itemCatsTwo);
                for (ItemCat x2 : itemCatsTwo){
                    queryWrapper.clear();
                    queryWrapper.eq("parent_id", x2.getId());
                    List<ItemCat> itemCatsThree = itemCatMapper.selectList(queryWrapper);
                    x2.setChildren(itemCatsThree);
                }
            }
        }else if(level == 2){
            for (ItemCat x : itemCatsOne){
                queryWrapper.clear();
                queryWrapper.eq("parent_id", x.getId());
                x.setChildren(itemCatMapper.selectList(queryWrapper));
            }
        }
        long EndTime = System.currentTimeMillis();
        System.out.println(EndTime-startTime);
        return itemCatsOne;
    }

    @Override
    public List<ItemCat> getItemCatList2(Integer level) {
        long startTime = System.currentTimeMillis();
        Map<Integer, List<ItemCat>> integerListMap = itemLM();
        List<ItemCat> itemCatsOne = integerListMap.get(0);
        if(level == 3){
            for (ItemCat x1 : itemCatsOne){
                List<ItemCat> twoList = integerListMap.get(x1.getId());
                x1.setChildren(twoList);
                if(twoList == null) continue;
                for (ItemCat x2 : twoList){
                    List<ItemCat> threeList = integerListMap.get(x2.getId());
                    x2.setChildren(threeList);
                }
            }
        }else if(level == 2){
            for (ItemCat x : itemCatsOne){
                x.setChildren(integerListMap.get(x.getId()));
            }
        }
        long EndTime = System.currentTimeMillis();
        System.out.println(EndTime-startTime);
        return itemCatsOne;
    }

    @Override
    public ItemCat updaStatus(Integer id, Boolean status) {
        ItemCat itemCat = new ItemCat();
        itemCat.setId(id).setStatus(status);
        itemCatMapper.updateById(itemCat);
        return null;
    }

    @Override
    @Transactional
    public ItemCat saveItemCat(ItemCat itemCat) {
        itemCatMapper.insert(itemCat);
        return null;
    }

    @Override
    public void deleteItemCat(ItemCat itemCat) {
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper();
//        queryWrapper.eq("id", itemCat.getId())
//                .eq("level", itemCat.getLevel());
//        itemCatMapper.delete(queryWrapper);
        int level = itemCat.getLevel();
        if(level == 3){
            itemCatMapper.deleteById(itemCat.getId());
            return;
        }else if(level == 2){
            queryWrapper.clear();
            queryWrapper.eq("parent_id", itemCat.getId());
            itemCatMapper.delete(queryWrapper);
            itemCatMapper.deleteById(itemCat.getId());
            return;
        }else if(level == 1){
            queryWrapper.clear();
            queryWrapper.eq("parent_id", itemCat.getId());
            List<ItemCat> TwoItemList = itemCatMapper.selectList(queryWrapper);
            for (ItemCat TwoItem : TwoItemList){
                queryWrapper.clear();
                queryWrapper.eq("parent_id", TwoItem.getId());
                itemCatMapper.delete(queryWrapper);
                itemCatMapper.deleteById(TwoItem.getId());
            }
            itemCatMapper.deleteById(itemCat.getId());
            return;
        }
    }

    @Override
    public void updateItemCat(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
        System.out.println(itemCat);
        return;
    }
}
